﻿using UnityEditor;
using UnityEngine;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {
        [CtxMenuItem("Renderer/Mesh/Name/Rename Renderer", "Renames a renderer to match it's mesh's name.", 0)]
        public static void RendererRename()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context.transform)) continue;
                Undo.RecordObject(context.gameObject, "Rename");
                context.transform.gameObject.name = SkinnEx.EnforceObjectName(context.GetSharedMesh());
            }
            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Mesh/Name/From Renderer", "Renames a mesh to match it's renderer's name.", 0)]
        public static void RendererRenameMesh()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (!SkinnEx.HasMinimumRequirements(context)) continue;
                SkinnContext.UndoRecord(context, "Rename");
                var mesh = context.GetSharedMesh().Clone() as Mesh;
                mesh.name = context.transform.name;
                context.SetSharedMesh(mesh);
            }
            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Mesh/Name/From Parent", "Renames a renderer's mesh to match it's parent name.", 0)]
        public static void RendererRenameMeshFromParent()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (!SkinnEx.HasMinimumRequirements(context)) continue;
                SkinnContext.UndoRecord(context, "Rename");
                var mesh = context.GetSharedMesh().Clone() as Mesh;
                mesh.name = context.transform.parent ? context.transform.parent.name : context.transform.name;
                context.SetSharedMesh(mesh);
            }
            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Mesh/Name/From Root", "Renames a renderer's mesh to match it's root name.", 0)]
        public static void RendererRenameMeshFromRoot()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (!SkinnEx.HasMinimumRequirements(context)) continue;
                SkinnContext.UndoRecord(context, "Rename");
                var mesh = context.GetSharedMesh().Clone() as Mesh;
                mesh.name = context.transform.root.name;
                context.SetSharedMesh(mesh);
            }
            SkinnContext.Finished();
        }
    }
}